home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / print / 890ps.arc / PSERVER.C < prev    next >
Text File  |  1989-12-18  |  5KB  |  187 lines

  1. /*  Program to service print jobs.  */
  2.  
  3.  
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <netware.h>
  8. #include <dos.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11.  
  12.  
  13. #define MODECOUNT 4
  14. typedef enum {PostScript,PostScriptInteractive,Diablo630,LaserJet} Modes;
  15.  
  16.  
  17. int stop()
  18. {
  19.   static int abort_flag = 0;
  20.  
  21.   return(abort_flag=(abort_flag || (kbhit() && (getch()==0x1B))));
  22. }
  23.  
  24.  
  25. void die(const char *msg1, const char *msg2, byte code)
  26. {
  27.   cprintf("Failed during %s%s (%0#2x).\n",msg1,msg2,code);
  28.   Logout();
  29.   exit(1);
  30. }
  31.  
  32.  
  33. void out(const char *data, unsigned len)
  34. {
  35.   union REGS i_regs,o_regs;
  36.  
  37.   i_regs.h.ah = 0x00;
  38.   i_regs.x.dx = 0x00;
  39.   while (len!=0) {
  40.     if (*data!=0) {
  41.       i_regs.h.al = *data;
  42.       do {
  43.         int86(0x17,&i_regs,&o_regs);
  44.       } while (((o_regs.h.ah&0x01)==1) && !(stop()));
  45.     }
  46.     data++;
  47.     len--;
  48.   }
  49. }
  50.  
  51.  
  52. void SetMode(Modes NewMode)
  53. {
  54.   static Modes CurrMode = PostScript;
  55.   const struct {
  56.           char PreMode[50];
  57.           unsigned PreWait;
  58.           unsigned MidWait;
  59.           char PostMode[50];
  60.           unsigned PostWait;
  61.           char Desc[25];
  62.         } ModeInfo[MODECOUNT] = {{"",0,0,"",0,"PostScript"},
  63.                                  {"statusdict begin 1 setsoftwareiomode end\x04",4000,10000,"statusdict begin 1 setsoftwareiomode end\x04",4000,"PostScript Interactive"},
  64.                                  {"statusdict begin 2 setsoftwareiomode end\x04",4000,10000,"\x1B\x7F\x30\x04",10000,"Diablo 630"},
  65.                                  {"statusdict begin 5 setsoftwareiomode end\x04",4000,10000,"\x1B\x7F\x30\x04",10000,"LaserJet"}};
  66.  
  67.   if (NewMode!=CurrMode) {
  68.     if (ModeInfo[CurrMode].MidWait!=0) {
  69.       delay(ModeInfo[CurrMode].MidWait);
  70.     }
  71.     cprintf("    Ending %s...\n",ModeInfo[CurrMode].Desc);
  72.     out(ModeInfo[CurrMode].PostMode,strlen(ModeInfo[CurrMode].PostMode));
  73.     if (ModeInfo[CurrMode].PostWait!=0) {
  74.       delay(ModeInfo[CurrMode].PostWait);
  75.     }
  76.     cprintf("    Switching to %s...\n",ModeInfo[NewMode].Desc);
  77.     out(ModeInfo[NewMode].PreMode,strlen(ModeInfo[NewMode].PreMode));
  78.     if (ModeInfo[NewMode].PreWait!=0) {
  79.       delay(ModeInfo[NewMode].PreWait);
  80.     }
  81.     CurrMode = NewMode;
  82.   }
  83. }
  84.  
  85.  
  86. void Process(Modes mode, dword id)
  87. {
  88.   int file;
  89.   char buf[1024];
  90.   int len;
  91.   QueueJobInfoStruct info;
  92.   PrintJobClientStruct client;
  93.  
  94.   while ((ServiceQueueJobAndOpenFile(id,0xFFFF,&info)==0) && !(stop())) {
  95.     SetMode(mode);
  96.     if((file=_open("NETQ",O_RDONLY|SH_NOINHERIT|SH_DENYRW))!=-1) {
  97.       while ((len=_read(file,buf,sizeof(buf)))>0) {
  98.         if (stop()) {
  99.           break;
  100.         }
  101.         out(buf,len);
  102.       }
  103.       _close(file);
  104.     }
  105.     if (stop()) {
  106.       AbortServicingQueueJobAndFile(id,info._JobNumber);
  107.       cputs("      Aborting...\n");
  108.     } else {
  109.       if ((ReadPrintJobInfo(id,info._JobNumber,&client)==0) && ((client._Flags&0x8)==0)) {
  110.         out("\x0C",1);
  111.       }
  112.       FinishServicingQueueJobAndFile(id,info._JobNumber,0);
  113.       cprintf("      Finished printing (%s->%s)...\n",client._BannerName,client._BannerFile);
  114.     }
  115.   }
  116.  
  117. }
  118.  
  119.  
  120. byte AttachQueueServerToPrintQueue(const char *name, dword *id)
  121. {
  122.   byte res;
  123.  
  124.   if ((res=GetBinderyObjectID(name,0x0003,id))==0) {
  125.     res=AttachQueueServerToQueue(*id);
  126.   }
  127.  
  128.   return(res);
  129. }
  130.  
  131.  
  132. main()
  133. {
  134.   byte res;
  135.   struct{
  136.     char Name[25];
  137.     dword ID;
  138.   } queue[MODECOUNT] = {{"POSTSCRIPT",0},
  139.                         {"POSTSCRIPTINTERACTIVE",0},
  140.                         {"DIABLO630",0},
  141.                         {"LASERJET",0}};
  142.   unsigned i,j;
  143.  
  144.   if ((res=LoginToFileServer("LC890",0x0007,"LC890"))!=0) {
  145.     die("login","",res);
  146.   }
  147.   cputs("LC890 printer server is logged on...\n");
  148.  
  149.   i = PostScript;
  150.   while (i<MODECOUNT) {
  151.     if ((res=AttachQueueServerToPrintQueue(queue[i].Name,&queue[i].ID))!=0) {
  152.       j = PostScript;
  153.       while (j<i) {
  154.         DetachQueueServerFromQueue(queue[j].ID);
  155.         j++;
  156.       }
  157.       die("attach to ",queue[i].Name,res);
  158.     } else {
  159.       cprintf("Attached to %s\n",queue[i].Name);
  160.     }
  161.     i++;
  162.   }
  163.  
  164.   while (!stop()) {
  165.     cputs("  Looping...\n");
  166.     for(i=PostScript;i<MODECOUNT;i++) {
  167.       Process(i,queue[i].ID);
  168.       if (stop()) {
  169.         break;
  170.       }
  171.     }
  172.     cputs("  Pausing...\n");
  173.     delay(10000);
  174.   }
  175.  
  176.   cputs("Detaching...\n");
  177.   for (i=PostScript;i<MODECOUNT;i++) {
  178.     DetachQueueServerFromQueue(queue[i].ID);
  179.   }
  180.  
  181.   cputs("Logging out...\n");
  182.   Logout();
  183.  
  184.   cputs("Done.\n");
  185.   return(0);
  186. }
  187.